package com.pbxadmin.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pbxadmin.cfg.WSConstants;
import com.pbxadmin.pojo.RxFax;
import com.pbxadmin.pojo.SipPeer;
import com.pbxadmin.service.PbxWebService;

@Controller
@RequestMapping("/rxfax")
public class RxFaxController {
	
	private Logger log = LoggerFactory.getLogger(RxFaxController.class);
	@Autowired
	private PbxWebService pbxWebService;
	@Autowired
	private Gson gson;
	/**
	 * 列表
	 */ 
	@RequestMapping("/list")
	public ModelAndView list(){
		String returnCode = pbxWebService.invokePbx("list_rxfax");
		if(returnCode.isEmpty()){
			return new ModelAndView("/error/pbxerror","message","pbxservice 发生错误，请稍候再试。");
		}
		
		Map<String,RxFax> rxfaxMap = gson.fromJson(returnCode, new TypeToken<Map<String,RxFax>>(){}.getType());
		
		ModelAndView mav = new ModelAndView("/rxfax/list");
		mav.addObject("rxfaxMap",rxfaxMap);
		return mav;
	}
	/**
	 * 修改初始化
	 */
	@RequestMapping("/modify/{rxfaxId}/init")
	public ModelAndView modifyInit(HttpServletRequest req,@PathVariable("rxfaxId") String rxfaxId){
		//
		if(rxfaxId == null || rxfaxId.isEmpty()){
			return new ModelAndView("/error/parameters","message","/rxfax/detail/{rxfaxId} , rxfaxId is null");
		}
		//
		String result = req.getParameter("result");
		if(result != null){
			result = new String(Base64.decodeBase64(result));
		}else{
			result = "";
		}
		//
		String returnCode = pbxWebService.invokePbx("list_rxfax");
		Map<String,RxFax> rxfaxMap = gson.fromJson(returnCode, new TypeToken<Map<String,RxFax>>(){}.getType());
		RxFax rxfax = rxfaxMap.get(rxfaxId);
		//
		ModelAndView mav = new ModelAndView("/rxfax/modify");
		mav.addObject("rxfaxId", rxfaxId);
		mav.addObject("rxfax", rxfax);
		mav.addObject("result", result);
		return mav;
	}
	/**
	 * 修改
	 */
	@RequestMapping("/modify/{rxfaxId}")
	public ModelAndView modify(HttpServletRequest req,@PathVariable("rxfaxId") String rxfaxId,@ModelAttribute RxFax rxfax){
		if(rxfaxId == null || rxfaxId.isEmpty()){
			return new ModelAndView("/error/parameters","message","/rxfax/detail/{rxfaxId} , rxfaxId is null");
		}
		
		String returnCode = pbxWebService.invokePbx("mod_rxfax", new Object[]{rxfaxId,rxfax.getPath()});
		String result = "";
		if(WSConstants.RTN_SUCCESS.equals(returnCode)){
			result = "修改传真["+rxfaxId+"]成功";
		}else{
			result = "修改传真["+rxfaxId+"]失败";
		}
		result = Base64.encodeBase64URLSafeString(result.getBytes());
		return new ModelAndView(new RedirectView("/pbxadmin/rxfax/modify/"+rxfaxId+"/init"),"result",result);
	}
}
